#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;
int i=0;
void lexical(FILE *fp) {
	char x = getc(fp);
    num = 0;
    strcpy(id, "\0");
    for(;i<20;i++)
        id[i]=NULL;
    i=0;
    while(x==' '||x=='\n'||x=='\t')
    {
         x=getc(fp);
    }
    if((x>='a'&&x<='z')||(x>='A'&&x<='Z'))
    {
        while(((x<='z')&&(x>='a'))||((x<='Z')&&(x>='A'))||((x>='0')&&(x<='9')))
        {
            id[i++]=x;
            x=getc(fp);
        }
        ungetc(x,fp);
        token=10;
	int n;
        for(n=0;n<6;n++)
        {
            if(strcmp(id,keyword[n])==0)
            {
                token=n+1;
                break;
            }
        }
    }
    else if((x>='0')&&(x<='9'))
    {
        while((x>='0')&&(x<='9'))
       {
            num=num*10+x-'0';
            x=getc(fp);
       }
       ungetc(x,fp);
       token=11;
    }
    else
    switch(x)
    {
    case '<':
        id[i++]=x;
        x=getc(fp);
        if(x=='=')
        {
            token=22;
            id[i++]=x;
        }
        else
        {
            token=20;
            ungetc(x,fp);
        }
        break;
    case '>':
        id[i++]=x;
        x=getc(fp);
        if(x=='=')
        {
            token=24;
            id[i++]=x;
        }
        else
        {
            token=23;
            ungetc(x,fp);
        }
        break;
    case '+':
        id[i++]=x;
        x=getc(fp);
        if(x=='+')
        {
            token=17;
            id[i++]=x;
        }
        else
        {
            token=13;
            ungetc(x,fp);
        }
        break;
    case '-':
        id[i++]=x;
        x=getc(fp);
        if(x=='-')
        {
            token=29;
            id[i++]=x;
        }
        else
        {
            token=14;
            ungetc(x,fp);
        }
        break;
    case '!':
        id[i++]=x;
        x=getc(fp);
        if(x=='=')
        {
            token=21;
            id[i++]=x;
        }
        else
        {
            token=31;
            ungetc(x,fp);
        }
        break;
    case ':':
        id[i++]=x;
        x=getc(fp);
        if(x=='=')
        {
            token=18;
            id[i++]=x;
        }
        else
        {
            token=17;
            ungetc(x,fp);
        }
        break;
    case '=':
        id[i++]=x;
        x=getc(fp);
        if(x=='=')
        {
            token=25;
            id[i++]=x;
        }
        else
        {
            token=18;
            ungetc(x,fp);
        }
        break;
    case '*':
            token=15;
            id[i++]=x;
            break;
    case '/':
            token=16;
            id[i++]=x;
            break;
    case '(':
            token=27;
           id[i++]=x;
            break;
    case ')':
            token=28;
            id[i++]=x;
            break;
    case '{':
            token=5;
            id[i++]=x;
            break;
    case '}':
            token=6;
           id[i++]=x;
            break;
    case ';':
            token=26;
            id[i++]=x;
            break;
     case '#':
            token=0;
            id[i++]=x;
            break;
       default:
            token=-1;
            break;
    }
    id[i]='\0';

	//process x
	//process each regex here
	//store the result in global variables id, num, or token
	//in case of backtrack, use
	//ungetc(x, fp);

}

int main(int argc, const char *argv[])
{
	if(argc < 2) {
		printf("insufficient args\n");
		return 1;
	}
	FILE *fp = fopen(argv[1], "r");
	do {
		lexical(fp);
		switch(token) {
			case 10: printf("(id, %s)\n", id); break;
			case 11: printf("(num, %d)\n", num); break;
			case -1: printf("error!\n"); break;
			default: printf("(%d, %s)\n", token, id);
		}
	} while(token != 0);


	fclose(fp);
	return 0;
}
